<?php

App::Import('Core', 'ConnectionManager');

class FtpfilesController extends AppController {

    var $name = 'Ftpfiles';
    var $uses = array();
    var $helpers = array('Ftpman', 'Javascript', 'Session');
    var $components = array('Auth', 'Session', 'Acl');
	var $view = 'Theme';    
	var $theme = 'software';
	
    function __construct() {
        parent::__construct();
        $this->ftpman = ConnectionManager::getDataSource('ftpman');
    }
    function beforeFilter() {
        parent::beforeFilter(); 
        $this->Auth->allowedActions = array('*');
    }

    /**
     * 显示入口页面
     */
    function entry() {
        $this->layout = 'default_entry';
    }
    
    /**
     * 热门下载的显示页面
     */
    function hotfiles() {
         $this->pageTitle  = '热门下载';
         $hotfilesPath = '';
         $RecommendedFilesPath = $hotfilesPath.DS.'hotdownloads';
         $CategoriesPath = $hotfilesPath.DS.'SoftwareCategories';
         
         //get the RecommendedFiles information
         $RecommendedFilesInfo = $this->ftpman->get($RecommendedFilesPath);
         $RecommendedFiles = $RecommendedFilesInfo->ls();
         $this->set('RecommendedFiles', $RecommendedFiles['files']);
         
         //get the Categories information
         $CategoriesInfo = $this->ftpman->get($CategoriesPath);
         $Categories = $CategoriesInfo->ls();
         $this->set('Categories', $Categories['dirs']);
         
         
         //get the Categories files information
         $CategoriesFiles = array();
         foreach( $Categories['dirs'] as $name => $entry ) {
            $filesinfo = $this->ftpman->get($entry->filename);
            $files = $filesinfo->ls();
            $CategoriesFiles[$name] = $files['files'];            
         }
         $this->set('CategoriesFiles', $CategoriesFiles);
    }
    
    /**
     * 显示文件列表
     */
    function index($path = '',$page = 1) {
        $this->pageTitle  = '文件浏览';
        $path = base64_decode($path);
        $this->set("Acl",$this->Acl);
        // if($this->Session->read('Auth')) {
            // $nowUser = $this->Auth->user();
            // if($this->Acl->check(array('model' => 'Group', 'foreign_key' => $nowUser['User']['group_id']), "Users/admin_viewUsers"))
                // $this->set('showEntry', true);
            // else
                // $this->set('showEntry', false);
        // } else {
            // $this->set('showEntry', false);
        // }

        $currentEntry = $this->ftpman->get($path);
        $this->set('currentEntry', $currentEntry);

        if ($parentEntry = $currentEntry->get_parent_entry()) {
            $this->set('parentEntry', $parentEntry);
        }

        $children = $currentEntry->ls();
        $this->set('subdirs', $children['dirs']);
        $this->set('childEntries', $children['files']);

        $view = 'index';
        if ($currentEntry->View) {
            $view .= $currentEntry->View;
        }
        $this->render($view);
    }

    /**
     * 显示文件详细信息
     */
    function detail($path) {
        $path = base64_decode($path);
        $entry = $this->ftpman->get($path);
        $this->set('entry', $entry);
        $parentEntry = $entry->get_parent_entry();
        $this->set('parentEntry', $parentEntry);
        $this->pageTitle = $entry->Name;
    }

    /**
     * 显示文件夹的图标，如果该文件夹没有设置图标，则显示默认图标。
     */
    function icon($path) {
        $this->layout = 'ajax';
        $path = base64_decode($path);
        $entry = $this->ftpman->get($path);
        $icon_path = $entry->get_icon_path();
        $this->set('icon_path', $icon_path);
    }

    /**
     * 跳转到 FTP 下载的地址
     */
    function download_from_ftp($path) {
        $path = base64_decode($path);
		$entry = $this->ftpman->get($path);
		$count = $entry->DownloadCount;
		$entry->set('DownloadCount', $count+1);
        $this->redirect($this->ftpman->url($path), 302);
    }

    /**
     * 把目录作为 Zip 文件下载
     */
    function download_as_zip($path) {
        $path = base64_decode($path);

        $this->autoRender = false;
        $this->debug = false;
        $entry = $this->ftpman->get($path);
        $files = $entry->find();

        App::Import('Vendor', 'zipstream');
        $zipname = basename($path).'.zip';
        # create new zip stream object
        $zip = new ZipStream($zipname, array());

        # common file options
        $file_opt = array();

        # add files under folder 'asdf'
        foreach ($files as $entryname => $entrypath) {
            # add file to archive
            $zip->add_file_from_path($entryname, $entrypath, $file_opt);
        }

        # finish archive
        $zip->finish();
    }

    /**
     * 删除一个文件项。只有叶子项目可以被删除，目录不可以被删除。
     */
    function unlink($path, $json = false) {
        $path = base64_decode($path);

        if (!$json) {
            $ppath = substr($path, 0, strrpos($path, '/'));
            $url = '/ftpfiles/index/'.base64_encode($ppath);
        }

        try {
            $entry = $this->ftpman->get($path);
            $entry->unlink();
        } catch (Exception $e) {
            $message = "未能删除 $path: ".$e->getMessage();
            if ($json) {
                $this->view = 'Json';
                $this->set('failure', true);
                $this->set('message', $message);
                $this->set('json', array('failure', 'message'));
            } else {
                $this->Session->setFlash($message, 'default', array('class' => 'failure'));
                $this->redirect($url);
            }
            return;
        }

        $message = "已删除 $path";
        if ($json) {
            $this->view = 'Json';
            $this->set('success', true);
            $this->set('message', $message);
            $this->set('json', array('success', 'message'));
        } else {
            $this->Session->setFlash($message, 'default', array('class' => 'success'));
            $this->redirect($url);
        }
		
    }
	
    
}
?>
